﻿@inherits BaseComponent
@typeparam TItem where TItem : class, new()

<ReloadContainer @ref="reload">
    <AntTable @ref="table" TItem="TItem" Model="Model" ScrollX="@ScrollX" ScrollY="@ScrollY"
              ExpandTemplate="ExpandTemplate" OnExpand="OnExpand" OnRow="OnRow" OnChange="OnChange"
              Total="totalCount" DataSource="dataSource" Loading="Model.EnableSpin && isQuering"
              @bind-SelectedRows="Model.SelectedRows">
        <ColumnDefinitions>
            @if (Context.IsEditTable) {
                <TableColumn TitleTemplate="ColumnTitle" Width="40" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Left" />
            }
            @if (Model.SelectType != TableSelectType.None) {
                <Selection Type="@GetSelectionType(Model.SelectType)" Align="ColumnAlign.Center" Width="40" Fixed="ColumnFixPlacement.Left" />
            }
            @if (Model.Columns != null && Model.Columns.Count > 0) {
                var isDictionary = Model.IsDictionary;
                if (Model.ShowIndex) {
                    var columnId = Model.Columns[0].Id;
                    if (isDictionary) {
                        @*<DictionaryColumn Title="@Language.Index" Property="c=>c.GetValue(columnId)" Align="ColumnAlign.Center" Width="50" Fixed="ColumnFixPlacement.Left">
                            <span>@GetIndex(context)</span>
                        </DictionaryColumn>*@
                    } else {
                        <TableColumn Title="@Language.Index" Align="ColumnAlign.Center" Width="50" Fixed="ColumnFixPlacement.Left">
                            <span>@GetIndex(context)</span>
                        </TableColumn>
                    }
                }
                var columns = GetColumns();
                foreach (var item in columns) {
                    if (string.IsNullOrWhiteSpace(item.Id))
                        continue;

                    var title = Language?.GetFieldName<TItem>(item);
                    var width = item.Width > 0 ? item.Width.ToString() : "";
                    RenderFragment<TItem> template = null;
                    Model.Templates?.TryGetValue(item.Id, out template);
                    if (isDictionary) {
                        var data = context as Dictionary<string, object>;
                        var value = data?.GetValue(item.Id);
                        <DictionaryColumn Title="@title" TitleTemplate="@DataTitle(item, title)" Property="c=>c.GetValue(item.Id)" Width="@width"
                                          Ellipsis="item.ToEllipsis()" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                          ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                          ColSpan="@GetRowSpan(item, value)" RowSpan="@GetColSpan(item, value)"
                                          FilterDropdown="@GetFilterTemplate(item)">
                            @if (template != null) {
                                @template(context)
                            } else {
                                @if (item.Type == FieldType.Switch) {
                                    var isChecked = Utils.ConvertTo<bool>(value);
                                    <Switch Disabled Value="isChecked" />
                                } else if (item.Type == FieldType.File) {
                                    <KFileCell Value="@value?.ToString()" />
                                } else {
                                    var text = GetColumnText(item, value);
                                    if (item.IsViewLink) {
                                        <KLink OnClick="e=>OnViewForm(context)">@text</KLink>
                                    } else if (item.LinkAction != null) {
                                        <KLink OnClick="e=>OnLinkAction(item, context)">@text</KLink>
                                    } else {
                                        <span>@text</span>
                                    }
                                }
                            }
                        </DictionaryColumn>
                    } else {
                        if (typeof(TItem).HasProperty(item.Id)) {
                            var value = TypeHelper.GetPropertyValue(context, item.Id);
                            <Column Title="@title" TitleTemplate="@DataTitle(item, title)" DataIndex="@item.Id" TData="object" Width="@width"
                                    Ellipsis="item.ToEllipsis()" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                    ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                    ColSpan="@GetRowSpan(item, value)" RowSpan="@GetColSpan(item, value)"
                                    FilterDropdown="@GetFilterTemplate(item)">
                                @if (template != null) {
                                    @template(context)
                                } else {
                                    @if (item.Type == FieldType.Switch) {
                                        var isChecked = Utils.ConvertTo<bool>(value);
                                        <Switch Disabled Value="isChecked" />
                                    } else if (item.Type == FieldType.File) {
                                        <KFileCell Value="@value?.ToString()" />
                                    } else {
                                        var text = GetColumnText(item, value);
                                        if (item.IsViewLink) {
                                            <KLink OnClick="e=>OnViewForm(context)">@text</KLink>
                                        } else if (item.LinkAction != null) {
                                            <KLink OnClick="e=>OnLinkAction(item, context)">@text</KLink>
                                        } else {
                                            <span>@text</span>
                                        }
                                    }
                                }
                            </Column>
                        } else {
                            <Column Title="@title" TitleTemplate="@DataTitle(item, title)" TData="object" Width="@width"
                                    Ellipsis="item.ToEllipsis()" Fixed="@item.ToColumnFixPlacement()" Align="@item.ToColumnAlign()"
                                    ShowSorterTooltip="false" Sortable="GetSortable(item)" DefaultSortOrder="item.ToSortDirection()"
                                    ColSpan="@GetRowSpan(item, item.Id)" RowSpan="@GetColSpan(item, item.Id)"
                                    FilterDropdown="@GetFilterTemplate(item)">
                                <span>@item.Id</span>
                            </Column>
                        }
                    }
                }
            }
            @if (Model.HasAction) {
                var actions = Model.RowActions?.Invoke(context);
                if (actions == null)
                    actions = Model.Actions;
                Model.UpdateRowActions?.Invoke(context, actions);
                actions = actions?.Where(a => a.Visible).ToList();
                <ActionColumn TitleTemplate="ActionTitle" Width="@Model.ActionWidth" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Right">
                    @if (actions.Count > Model.ActionCount) {
                        var takes = actions.Skip(0).Take(Model.ActionCount - 1).ToList();
                        var mores = actions.Skip(Model.ActionCount - 1).ToList();
                        foreach (var item in takes) {
                            if (!string.IsNullOrWhiteSpace(item.Name)) {
                                <KButton Name="@item.Name" Enabled="item.Enabled" Type="item.ToType()" Danger="item.IsDanger()" Title="@item.Title" Badge="item.Badge" OnClick="e => OnActionClick(item, context)" />
                            } else if (!string.IsNullOrWhiteSpace(item.Icon)) {
                                <KIcon Icon="@item.Icon" Enabled="item.Enabled" Title="@item.Title" IsTheme OnClick="this.Callback<MouseEventArgs>(e => OnActionClick(item, context))" />
                            } else {
                                <KButton Name="@item.Id" Enabled="item.Enabled" Type="item.ToType()" Danger="item.IsDanger()" Title="@item.Title" Badge="item.Badge" OnClick="e => OnActionClick(item, context)" />
                            }
                        }
                        var model = new DropdownModel {
                            Text = Language.More, Items = mores,
                            OnItemClick = e => OnActionClick(e, context)
                        };
                        <AntDropdown Context="Context" Model="model" />
                    } else {
                        foreach (var item in actions) {
                            if (!string.IsNullOrWhiteSpace(item.Name)) {
                                <KButton Name="@item.Name" Enabled="item.Enabled" Type="item.ToType()" Danger="item.IsDanger()" Title="@item.Title" Badge="item.Badge" OnClick="e => OnActionClick(item, context)" />
                            } else if (!string.IsNullOrWhiteSpace(item.Icon)) {
                                <KIcon Icon="@item.Icon" Enabled="item.Enabled" Title="@item.Title" IsTheme OnClick="this.Callback<MouseEventArgs>(e => OnActionClick(item, context))" />
                            } else {
                                <KButton Name="@item.Id" Enabled="item.Enabled" Type="item.ToType()" Danger="item.IsDanger()" Title="@item.Title" Badge="item.Badge" OnClick="e => OnActionClick(item, context)" />
                            }
                        }
                    }
                </ActionColumn>
            } else if (Context.IsEditTable) {
                <ActionColumn TitleTemplate="ActionTitle" Width="@Model.ActionWidth" Align="ColumnAlign.Center" Fixed="ColumnFixPlacement.Right" />
            }
            @if (Model.HasSum) {
                <SummaryRow>
                    @if (Context.IsEditTable) {
                        <SummaryCell />
                    }
                    @if (Model.SelectType != TableSelectType.None) {
                        <SummaryCell />
                    }
                    @if (Model.ShowIndex) {
                        <SummaryCell />
                    }
                    @if (Model.Columns != null && Model.Columns.Count > 0) {
                        var isTitle = false;
                        var columns = GetColumns();
                        foreach (var item in columns) {
                            if (string.IsNullOrWhiteSpace(item.Id))
                                continue;

                            if (!isTitle) {
                                <SummaryCell Align="@item.ToColumnAlign()">
                                    @if (Model.ShowPager) {
                                        @Language[Language.SumPage]<br/>
                                    }
                                    <strong>@Language[Language.SumQuery]</strong>
                                </SummaryCell>
                                isTitle = true;
                                continue;
                            }

                            if (item.IsSum) {
                                var value = Model.Result?.GetPageSum(item.Id);
                                var statis = Model.Result?.Statis?.First(s => s.Key.Equals(item.Id, StringComparison.OrdinalIgnoreCase));
                                var total = statis?.Value;
                                <SummaryCell Align="@item.ToColumnAlign()">
                                    @if (Model.ShowPager) {
                                        @value<br/>
                                    }
                                    <strong>@total</strong>
                                </SummaryCell>
                            } else {
                                <SummaryCell />
                            }
                        }
                    }
                    @if (Model.HasAction) {
                        <SummaryCell />
                    }
                </SummaryRow>
            }
        </ColumnDefinitions>
    </AntTable>
</ReloadContainer>

@code {
    RenderFragment ActionTitle => @<span>
        <span>@Language[Language.Action]</span>
        @if (Context.IsEditTable) {
            <Tooltip Title="@Language[Language.AddAction]" Class="kui-edit" Style="margin-left:10px;display:inline-block;">
                <KIcon Icon="plus" OnClick="this.Callback<MouseEventArgs>(e => OnAddAction())" />
            </Tooltip>
        }
    </span>;

    RenderFragment ColumnTitle => @<Tooltip Title="@Language[Language.AddColumn]" Class="kui-edit">
        <KIcon Icon="plus" OnClick="this.Callback<MouseEventArgs>(e => OnAddColumn())" />
    </Tooltip>;

    RenderFragment DataTitle(ColumnInfo item, string title) => @<span>
        @if (!string.IsNullOrWhiteSpace(item.Tooltip)) {
            <Tooltip Title="@item.Tooltip">@Language[title]</Tooltip>
        } else {
            @Language[title]
        }
    </span>;
}